import com.magist.manage.web.WebApplication;
import com.magist.manage.web.entity.SysMenu;
import com.magist.manage.web.service.SysMenuService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.CollectionUtils;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class BuildMenuUtil {

    @Autowired
    private SysMenuService sysMenuService;

    @Test
    public void buildMenu() {

        String[] models = {"版权引入合同","版权输出合同"};
        String[] models_router = {"supplyContractIn","supplyContractOut"};

        String templateId = "18071800001008031";
        for (int i = 0; i < models.length; i++) {
            String model = models[i];
            String baseRouter = models_router[i];
//            this.doBuild(templateId,model,baseRouter);
        }
    }

    private void doBuild(String templateId, String model, String baseRouter) {
        // root
        SysMenu rootMenu = sysMenuService.getById(templateId);
        String rootId = sysMenuService.getNewId();
        String rootName = model+ "管理";
        String rootRemark = rootName;
        String rootRouterPath = "/" + baseRouter;
        String rootRouterName = baseRouter;
        rootMenu.setName(rootName);
        rootMenu.setRemark(rootRemark);
        rootMenu.setRouterPath(rootRouterPath);
        rootMenu.setRouterName(rootRouterName);
        rootMenu.setId(rootId);
        sysMenuService.insert(rootMenu);
        //menu
        List<SysMenu> menus = sysMenuService.findByParentId(templateId);
        menus.forEach(menu-> {
            String tempMenuId = menu.getId();
            String menuId = sysMenuService.getNewId();
            String menuName = "";
            String menuRemark = "";
            String menuRouterPath = "";
            String menuRouterName = "";
            if (menu.getName().contains("列表")){
                menuName = model + "列表";
                menuRouterPath = rootRouterPath + "/table";
                menuRouterName = baseRouter+ "Table";
            } else if(menu.getName().contains("新增")) {
                menuName = model + "新增";
                menuRouterPath = rootRouterPath + "/add";
                menuRouterName = baseRouter+ "Add";
            }
            menuRemark = menuName;
            menu.setId(menuId);
            menu.setParentId(rootId);
            menu.setName(menuName);
            menu.setRemark(menuRemark);
            menu.setRouterPath(menuRouterPath);
            menu.setRouterName(menuRouterName);
            sysMenuService.insert(menu);

            // 子按钮
            List<SysMenu> childButton = sysMenuService.findByParentId(tempMenuId);
            if (!CollectionUtils.isEmpty(childButton)) {
                childButton.forEach(button-> {
                    String[] split = button.getRouterName().split("_");
                    String suff = split[split.length - 1];
                    button.setId(sysMenuService.getNewId());
                    button.setParentId(menuId);
                    button.setRouterName(menu.getRouterName() + "_" + suff);
                    sysMenuService.insert(button);
                });
            }
        });
    }
}
